/*
 * Copyright 2010 Cedric Priscal
 *
 * This file is part of stupid-avr-robot.
 *
 * stupid-avr-robot is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * stupid-avr-robot is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with stupid-avr-robot.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef PACKET_H_
#define PACKET_H_

#include "Usart.h"
#include "UsartListener.h"
#include "PacketListener.h"

class Packet : public UsartListener {

private:
	static const unsigned char ESCAPE	= '\\';
	static const unsigned char START	= 's';
	static const unsigned char STOP		= '\n';

	Usart* 			mUsart;
#ifdef linux
	unsigned char 	mBuffer[512];
#else
	unsigned char 	mBuffer[64];
#endif
	unsigned char	mSize;
	bool 			mStarted;
	bool 			mEscape;
	PacketListener* mListener;

public:
	Packet(Usart *usart);
	void registerListener(PacketListener* listener);
	void sendPacket(const unsigned char *buffer, char size);
	void onUsartReceived(unsigned char byte);
};

#endif /* PACKET_H_ */
